local skel = fk.CreateSkill {
  name = "efengqi__jiyuan",
}

Fk:loadTranslationTable{
  ["efengqi__jiyuan"] = "急援",
  [":efengqi__jiyuan"] = "当你需要使用基本牌时，可以令一名角色摸一张牌，其可代替你使用之。若为你回合内且其未使用，则“急援”本回合失效。",

  ["#efengqi__jiyuan"] = "急援：声明你要使用的基本牌和目标，令一名角色摸一张牌，请求其替你使用此牌",
  ["#efengqi__jiyuan-choose"] = "急援：令一名角色摸一张牌，请求其替你使用【%arg】，取消则选自己",
  ["#efengqi__jiyuan-ask"] = "急援：是否替 %src 使用一张【%arg】？",

  ["$efengqi__jiyuan1"] = "公若辞，必遭蔡瑁之害矣。",
  ["$efengqi__jiyuan2"] = "形势危急，还请速行。",
}

skel:addEffect("viewas", {
  anim_type = "support",
  pattern = ".|.|.|.|.|basic",
  prompt = "#efengqi__jiyuan",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(skel.name, all_names)
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    if Fk.all_card_types[self.interaction.data] == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:playCardEmotionAndSound(player, use.card)
    if use.tos and #use.tos > 0 then
      room:doIndicate(player, use.tos)
    end
    local to = player
    local tos = room:askToChoosePlayers(player, {
      targets = room.alive_players, min_num = 1, max_num = 1,
      prompt = "#efengqi__jiyuan-choose:::" .. use.card.name,
      skill_name = self.name, cancelable = true,
    })
    if #tos > 0 then to = tos[1] end
    to:drawCards(1, self.name)
    local response
    if not to.dead then
      response = room:askToResponse(to, {
        skill_name = self.name, pattern = ".|.|.|.|"..use.card.name,
        prompt = "#efengqi__jiyuan-ask:"..player.id.."::"..use.card.name,
      })
    end
    if response then
      response.skipDrop = true
      room:responseCard(response)
      local card = response.card
      if not card:isVirtual() and room:getCardArea(card) == Card.Processing then
        use.card = card
      else
        local vcard = Fk:cloneCard(use.card.name)
        vcard.skillName = self.name
        vcard:addSubcards(room:getSubcardsByRule(card, {Card.Processing}))
        use.card = vcard
      end
    else
      if player == room.current then
        room:invalidateSkill(player, self.name, "-turn")
      end
      return self.name
    end
  end,
  enabled_at_play = function (self, player)
    return true
  end,
  enabled_at_response = function(self, player, response)
    return not response
  end,
})

return skel
